<?php

!defined('IN_ROOT') && exit('Access Denied');

class viewsmodel {

    var $db;
    var $base;

    function viewsmodel(&$base) {
        $this->base = $base;
        $this->db = $base->db;
    }


    function get($id) {
        return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."views WHERE id='$id'");
    }

    function get_list($start=0,$limit=10) {
        $viewslist=array();
        $query=$this->db->query("select * from ".DB_TABLEPRE."views order by time desc limit $start,$limit");
        while($views=$this->db->fetch_array($query)) {
            $views['time']=tdate($views['time'],3,0);
            $views['areaname'] = $this->db->result_first("SELECT name FROM `".DB_TABLEPRE."area` where id='$views[sid]' ");
            $rs = $this->db->query("select id,name,price,mprice from ".DB_TABLEPRE."ticket where sid='$views[id]'");
            $str = '';
            while($rt=$this->db->fetch_array($rs)) {
            	$str .= $rt[id].'|'.$rt[name].'|'.$rt[price].'|'.$rt[mprice].'@'; 
            }
            $views['ticket'] = substr($str,0,-1);
            $viewslist[]=$views;
        }
        //var_dump($viewslist);
        return $viewslist;
    }
    
    function get_ticket($vid) {
    	$rs = $this->db->query("select id,name,price,mprice from ".DB_TABLEPRE."ticket where sid='$vid'");
        $ticketarr = array();
        while($rt=$this->db->fetch_array($rs)) {
           $ticketarr[] = $rt;
        }
        return $ticketarr;
    }

    function get_by_range($from,$to,$start=0,$limit=10) {
        $viewslist = array();
        $query = $this->db->query("SELECT * FROM ".DB_TABLEPRE."views WHERE `credit`>=$from AND `credit`<=$to ORDER BY `time` DESC LIMIT $start,$limit");
        while($views=$this->db->fetch_array($query)) {
            $views['time']=tdate($views['time'],3,0);
            $viewslist[]=$views;
        }
        return $viewslist;
    }

    function get_by_range_name($pid,$name='',$start=0,$limit=10) {
        $viewslist = array();
        $rangesql ='';
        ((int)$pid>0) && $rangesql= "AND `sid`=$pid";
        $query = $this->db->query("SELECT * FROM ".DB_TABLEPRE."views WHERE  `name` LIKE '%$name%' $rangesql ORDER BY `time` DESC LIMIT $start,$limit");
        while($views=$this->db->fetch_array($query)) {
            $views['time']=tdate($views['time'],3,0);
            $viewslist[]=$views;
        }
        return $viewslist;
    }
    function add($sid,$name,$sname,$keywords,$descs,$adress,$tel,$pics) {
        $this->db->query('INSERT INTO '.DB_TABLEPRE."views(`sid` ,`name` ,`sname` ,`keywords` ,`descs` ,`adress` ,`tel` ,`pics` ,`time`) values ('$sid','$name','$sname','$keywords','$descs','$adress','$tel','$pics','{$this->base->time}')");
        return $this->db->insert_id();
    }

    function update($sid,$name,$sname,$keywords,$descs,$adress,$tel,$pics,$id) {
        $this->db->query('update  '.DB_TABLEPRE."views  set `sid`='$sid',`name`='$name',`sname`='$sname',`keywords`='$keywords',`descs`='$descs',`adress`='$adress',`tel`='$tel',`pics`='$pics' where id=$id ");
    }
	
    function remove_by_id($ids) {
        $this->db->query("DELETE FROM `".DB_TABLEPRE."views` WHERE `id` IN ($ids)");
    }

    function update_available($id , $available) {
        $this->db->query("UPDATE ".DB_TABLEPRE."views SET `available`=$available WHERE `id`in ($id)");
    }

    function addlog($uid,$gid,$username,$realname,$email,$phone,$address,$postcode,$viewsname,$qq,$notes,$credit) {

        $this->db->query("INSERT INTO ".DB_TABLEPRE."viewslog SET `uid`=$uid,`gid`=$gid,`notes`='$notes',`email`='$email',`qq`='$qq',`phone`='$phone',`postcode`='$postcode',`address`='$address',`username`='$username',`realname`='$realname',`viewsname`='$viewsname',`credit`=$credit,`time`=".$this->base->time);
    }

    function getlog($start=0,$limit=10) {
        $loglist = array();
        $query = $this->db->query("SELECT * FROM ".DB_TABLEPRE."viewslog ORDER BY `time` DESC LIMIT $start,$limit");
        while($log=$this->db->fetch_array($query)) {
            $log['time']=tdate($log['time'],3,0);
            $loglist[]=$log;
        }
        return $loglist;
    }

    function update_views_status($ids,$status=1) {
        $this->db->query("UPDATE ".DB_TABLEPRE."viewslog SET `status`=$status WHERE `status`!=$status AND `id` IN ($ids)");
    }

    function list_by_searchlog($pricerange,$viewsname,$username,$datestart,$dateend,$start=0,$limit=10) {
        $sql = "SELECT * FROM `".DB_TABLEPRE."viewslog` WHERE 1=1 ";
        $viewsname && $sql.=" AND `viewsname` LIKE '$viewsname%' ";
        $username && $sql.="AND `username` LIKE '$username%' ";
        $datestart && ($sql .= " AND `time` >= ".strtotime($datestart));
        $dateend && ($sql .=" AND `time` <= " .strtotime($dateend));
        if($pricerange && ($pricerange != 'all')) {
            $ranges = explode("-",$pricerange);
            print_r($ranges);
            $sql.=" AND `credit`>".intval($ranges[0])." AND `credit`<= ".intval($ranges[1]);
        }
        $sql.=" ORDER BY `time` DESC LIMIT $start,$limit";
        $viewsloglist = array();
        $query = $this->db->query($sql);
        while($log=$this->db->fetch_array($query)) {
            $log['time']=tdate($log['time'],3,0);
            $viewsloglist[]=$log;
        }
        return $viewsloglist;
    }

    function rownum_by_searchlog($pricerange,$viewsname,$username,$datestart,$dateend) {
        $condition = " 1=1 ";
        $viewsname && $condition.=" AND `viewsname` LIKE '$viewsname%' ";
        $username && $condition.="AND `username` LINKE '$username%' ";
        $datestart && ($condition .= " AND `time` >= ".strtotime($datestart));
        $dateend && ($condition .=" AND `time` <= " .strtotime($dateend));
        if($pricerange && ($pricerange != 'all')) {
            $ranges = explode("-",$pricerange);
            $condition.=" AND `credit`>$range[0] AND `credit`<= $ranges[1] ";
        }

        return $this->db->fetch_total('viewslog',$condition);

    }


}
?>
